package com.escape.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.escape.auth.common.enums.IsDeletedEnum;
import com.escape.auth.domain.entity.AuthRolePermissionBO;
import com.escape.auth.domain.service.AuthRolePermissionDomainService;
import com.escape.auth.infra.basic.entity.AuthRolePermission;
import com.escape.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-10  17:15
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    /**
     * 新增角色权限
     */
    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRolePermissionDomainServiceImpl.add.DTO: {}", JSON.toJSONString(authRolePermissionBO));
        }
        LinkedList<AuthRolePermission> authRolePermissionLinkedList = new LinkedList<AuthRolePermission>();
        Long roleId = authRolePermissionBO.getRoleId();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            authRolePermissionLinkedList.add(authRolePermission);
        });
        Integer count = this.authRolePermissionService.insertBatch(authRolePermissionLinkedList);

        return count > 0;
    }
}
